﻿#region Using directives

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;

using GPSTrackingDBV2.Entities;
using GPSTrackingDBV2.Data;

#endregion

namespace GPSTrackingDBV2.Data.Bases
{	
	///<summary>
	/// This class is the base class for any <see cref="ScheduleWorkProviderBase"/> implementation.
	/// It exposes CRUD methods as well as selecting on index, foreign keys and custom stored procedures.
	///</summary>
	public abstract partial class ScheduleWorkProviderBaseCore : EntityProviderBase<GPSTrackingDBV2.Entities.ScheduleWork, GPSTrackingDBV2.Entities.ScheduleWorkKey>
	{		
		#region Get from Many To Many Relationship Functions
		#region GetByCompanyIdFromScheduleWorkByCompanies
		
		/// <summary>
		///		Gets ScheduleWork objects from the datasource by CompanyID in the
		///		ScheduleWorkByCompanies table. Table ScheduleWork is related to table Companies
		///		through the (M:N) relationship defined in the ScheduleWorkByCompanies table.
		/// </summary>
		/// <param name="_companyId"></param>
		/// <returns>Returns a typed collection of ScheduleWork objects.</returns>
		public TList<ScheduleWork> GetByCompanyIdFromScheduleWorkByCompanies(System.Int32 _companyId)
		{
			int count = -1;
			return GetByCompanyIdFromScheduleWorkByCompanies(null,_companyId, 0, int.MaxValue, out count);
			
		}
		
		/// <summary>
		///		Gets GPSTrackingDBV2.Entities.ScheduleWork objects from the datasource by CompanyID in the
		///		ScheduleWorkByCompanies table. Table ScheduleWork is related to table Companies
		///		through the (M:N) relationship defined in the ScheduleWorkByCompanies table.
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_companyId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a TList of ScheduleWork objects.</returns>
		public TList<ScheduleWork> GetByCompanyIdFromScheduleWorkByCompanies(System.Int32 _companyId, int start, int pageLength)
		{
			int count = -1;
			return GetByCompanyIdFromScheduleWorkByCompanies(null, _companyId, start, pageLength, out count);
		}
		
		/// <summary>
		///		Gets ScheduleWork objects from the datasource by CompanyID in the
		///		ScheduleWorkByCompanies table. Table ScheduleWork is related to table Companies
		///		through the (M:N) relationship defined in the ScheduleWorkByCompanies table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_companyId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of ScheduleWork objects.</returns>
		public TList<ScheduleWork> GetByCompanyIdFromScheduleWorkByCompanies(TransactionManager transactionManager, System.Int32 _companyId)
		{
			int count = -1;
			return GetByCompanyIdFromScheduleWorkByCompanies(transactionManager, _companyId, 0, int.MaxValue, out count);
		}
		
		
		/// <summary>
		///		Gets ScheduleWork objects from the datasource by CompanyID in the
		///		ScheduleWorkByCompanies table. Table ScheduleWork is related to table Companies
		///		through the (M:N) relationship defined in the ScheduleWorkByCompanies table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_companyId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of ScheduleWork objects.</returns>
		public TList<ScheduleWork> GetByCompanyIdFromScheduleWorkByCompanies(TransactionManager transactionManager, System.Int32 _companyId,int start, int pageLength)
		{
			int count = -1;
			return GetByCompanyIdFromScheduleWorkByCompanies(transactionManager, _companyId, start, pageLength, out count);
		}
		
		/// <summary>
		///		Gets ScheduleWork objects from the datasource by CompanyID in the
		///		ScheduleWorkByCompanies table. Table ScheduleWork is related to table Companies
		///		through the (M:N) relationship defined in the ScheduleWorkByCompanies table.
		/// </summary>
		/// <param name="_companyId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of ScheduleWork objects.</returns>
		public TList<ScheduleWork> GetByCompanyIdFromScheduleWorkByCompanies(System.Int32 _companyId,int start, int pageLength, out int count)
		{
			
			return GetByCompanyIdFromScheduleWorkByCompanies(null, _companyId, start, pageLength, out count);
		}


		/// <summary>
		///		Gets ScheduleWork objects from the datasource by CompanyID in the
		///		ScheduleWorkByCompanies table. Table ScheduleWork is related to table Companies
		///		through the (M:N) relationship defined in the ScheduleWorkByCompanies table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <param name="_companyId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a TList of ScheduleWork objects.</returns>
		public abstract TList<ScheduleWork> GetByCompanyIdFromScheduleWorkByCompanies(TransactionManager transactionManager,System.Int32 _companyId, int start, int pageLength, out int count);
		
		#endregion GetByCompanyIdFromScheduleWorkByCompanies
		
		#endregion	
		
		#region Delete Methods

		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to delete.</param>
		/// <returns>Returns true if operation suceeded.</returns>
		public override bool Delete(TransactionManager transactionManager, GPSTrackingDBV2.Entities.ScheduleWorkKey key)
		{
			return Delete(transactionManager, key.ScheduleWorkId);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="_scheduleWorkId">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public bool Delete(System.Int32 _scheduleWorkId)
		{
			return Delete(null, _scheduleWorkId);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_scheduleWorkId">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public abstract bool Delete(TransactionManager transactionManager, System.Int32 _scheduleWorkId);		
		
		#endregion Delete Methods
		
		#region Get By Foreign Key Functions
	
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ScheduleWork_DayNames key.
		///		FK_ScheduleWork_DayNames Description: 
		/// </summary>
		/// <param name="_dayId"></param>
		/// <returns>Returns a typed collection of GPSTrackingDBV2.Entities.ScheduleWork objects.</returns>
		public TList<ScheduleWork> GetByDayId(System.String _dayId)
		{
			int count = -1;
			return GetByDayId(_dayId, 0,int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ScheduleWork_DayNames key.
		///		FK_ScheduleWork_DayNames Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_dayId"></param>
		/// <returns>Returns a typed collection of GPSTrackingDBV2.Entities.ScheduleWork objects.</returns>
		/// <remarks></remarks>
		public TList<ScheduleWork> GetByDayId(TransactionManager transactionManager, System.String _dayId)
		{
			int count = -1;
			return GetByDayId(transactionManager, _dayId, 0, int.MaxValue, out count);
		}
		
			/// <summary>
		/// 	Gets rows from the datasource based on the FK_ScheduleWork_DayNames key.
		///		FK_ScheduleWork_DayNames Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_dayId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of GPSTrackingDBV2.Entities.ScheduleWork objects.</returns>
		public TList<ScheduleWork> GetByDayId(TransactionManager transactionManager, System.String _dayId, int start, int pageLength)
		{
			int count = -1;
			return GetByDayId(transactionManager, _dayId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ScheduleWork_DayNames key.
		///		fkScheduleWorkDayNames Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_dayId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of GPSTrackingDBV2.Entities.ScheduleWork objects.</returns>
		public TList<ScheduleWork> GetByDayId(System.String _dayId, int start, int pageLength)
		{
			int count =  -1;
			return GetByDayId(null, _dayId, start, pageLength,out count);	
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ScheduleWork_DayNames key.
		///		fkScheduleWorkDayNames Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_dayId"></param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of GPSTrackingDBV2.Entities.ScheduleWork objects.</returns>
		public TList<ScheduleWork> GetByDayId(System.String _dayId, int start, int pageLength,out int count)
		{
			return GetByDayId(null, _dayId, start, pageLength, out count);	
		}
						
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ScheduleWork_DayNames key.
		///		FK_ScheduleWork_DayNames Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_dayId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns a typed collection of GPSTrackingDBV2.Entities.ScheduleWork objects.</returns>
		public abstract TList<ScheduleWork> GetByDayId(TransactionManager transactionManager, System.String _dayId, int start, int pageLength, out int count);
		
	
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ScheduleWork_WorkTime key.
		///		FK_ScheduleWork_WorkTime Description: 
		/// </summary>
		/// <param name="_workTimeId"></param>
		/// <returns>Returns a typed collection of GPSTrackingDBV2.Entities.ScheduleWork objects.</returns>
		public TList<ScheduleWork> GetByWorkTimeId(System.Int32 _workTimeId)
		{
			int count = -1;
			return GetByWorkTimeId(_workTimeId, 0,int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ScheduleWork_WorkTime key.
		///		FK_ScheduleWork_WorkTime Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_workTimeId"></param>
		/// <returns>Returns a typed collection of GPSTrackingDBV2.Entities.ScheduleWork objects.</returns>
		/// <remarks></remarks>
		public TList<ScheduleWork> GetByWorkTimeId(TransactionManager transactionManager, System.Int32 _workTimeId)
		{
			int count = -1;
			return GetByWorkTimeId(transactionManager, _workTimeId, 0, int.MaxValue, out count);
		}
		
			/// <summary>
		/// 	Gets rows from the datasource based on the FK_ScheduleWork_WorkTime key.
		///		FK_ScheduleWork_WorkTime Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_workTimeId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of GPSTrackingDBV2.Entities.ScheduleWork objects.</returns>
		public TList<ScheduleWork> GetByWorkTimeId(TransactionManager transactionManager, System.Int32 _workTimeId, int start, int pageLength)
		{
			int count = -1;
			return GetByWorkTimeId(transactionManager, _workTimeId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ScheduleWork_WorkTime key.
		///		fkScheduleWorkWorkTime Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_workTimeId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of GPSTrackingDBV2.Entities.ScheduleWork objects.</returns>
		public TList<ScheduleWork> GetByWorkTimeId(System.Int32 _workTimeId, int start, int pageLength)
		{
			int count =  -1;
			return GetByWorkTimeId(null, _workTimeId, start, pageLength,out count);	
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ScheduleWork_WorkTime key.
		///		fkScheduleWorkWorkTime Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_workTimeId"></param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of GPSTrackingDBV2.Entities.ScheduleWork objects.</returns>
		public TList<ScheduleWork> GetByWorkTimeId(System.Int32 _workTimeId, int start, int pageLength,out int count)
		{
			return GetByWorkTimeId(null, _workTimeId, start, pageLength, out count);	
		}
						
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ScheduleWork_WorkTime key.
		///		FK_ScheduleWork_WorkTime Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_workTimeId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns a typed collection of GPSTrackingDBV2.Entities.ScheduleWork objects.</returns>
		public abstract TList<ScheduleWork> GetByWorkTimeId(TransactionManager transactionManager, System.Int32 _workTimeId, int start, int pageLength, out int count);
		
		#endregion

		#region Get By Index Functions
		
		/// <summary>
		/// 	Gets a row from the DataSource based on its primary key.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to retrieve.</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <returns>Returns an instance of the Entity class.</returns>
		public override GPSTrackingDBV2.Entities.ScheduleWork Get(TransactionManager transactionManager, GPSTrackingDBV2.Entities.ScheduleWorkKey key, int start, int pageLength)
		{
			return GetByScheduleWorkId(transactionManager, key.ScheduleWorkId, start, pageLength);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the primary key PK_ScheduleWork index.
		/// </summary>
		/// <param name="_scheduleWorkId"></param>
		/// <returns>Returns an instance of the <see cref="GPSTrackingDBV2.Entities.ScheduleWork"/> class.</returns>
		public GPSTrackingDBV2.Entities.ScheduleWork GetByScheduleWorkId(System.Int32 _scheduleWorkId)
		{
			int count = -1;
			return GetByScheduleWorkId(null,_scheduleWorkId, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_ScheduleWork index.
		/// </summary>
		/// <param name="_scheduleWorkId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="GPSTrackingDBV2.Entities.ScheduleWork"/> class.</returns>
		public GPSTrackingDBV2.Entities.ScheduleWork GetByScheduleWorkId(System.Int32 _scheduleWorkId, int start, int pageLength)
		{
			int count = -1;
			return GetByScheduleWorkId(null, _scheduleWorkId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_ScheduleWork index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_scheduleWorkId"></param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="GPSTrackingDBV2.Entities.ScheduleWork"/> class.</returns>
		public GPSTrackingDBV2.Entities.ScheduleWork GetByScheduleWorkId(TransactionManager transactionManager, System.Int32 _scheduleWorkId)
		{
			int count = -1;
			return GetByScheduleWorkId(transactionManager, _scheduleWorkId, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_ScheduleWork index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_scheduleWorkId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="GPSTrackingDBV2.Entities.ScheduleWork"/> class.</returns>
		public GPSTrackingDBV2.Entities.ScheduleWork GetByScheduleWorkId(TransactionManager transactionManager, System.Int32 _scheduleWorkId, int start, int pageLength)
		{
			int count = -1;
			return GetByScheduleWorkId(transactionManager, _scheduleWorkId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_ScheduleWork index.
		/// </summary>
		/// <param name="_scheduleWorkId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="GPSTrackingDBV2.Entities.ScheduleWork"/> class.</returns>
		public GPSTrackingDBV2.Entities.ScheduleWork GetByScheduleWorkId(System.Int32 _scheduleWorkId, int start, int pageLength, out int count)
		{
			return GetByScheduleWorkId(null, _scheduleWorkId, start, pageLength, out count);
		}
		
				
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_ScheduleWork index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_scheduleWorkId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns an instance of the <see cref="GPSTrackingDBV2.Entities.ScheduleWork"/> class.</returns>
		public abstract GPSTrackingDBV2.Entities.ScheduleWork GetByScheduleWorkId(TransactionManager transactionManager, System.Int32 _scheduleWorkId, int start, int pageLength, out int count);
						
		#endregion "Get By Index Functions"
	
		#region Custom Methods
		
		
		#endregion

		#region Helper Functions	
		
		/// <summary>
		/// Fill a TList&lt;ScheduleWork&gt; From a DataReader.
		/// </summary>
		/// <param name="reader">Datareader</param>
		/// <param name="rows">The collection to fill</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">number of rows.</param>
		/// <returns>a <see cref="TList&lt;ScheduleWork&gt;"/></returns>
		public static TList<ScheduleWork> Fill(IDataReader reader, TList<ScheduleWork> rows, int start, int pageLength)
		{
			NetTiersProvider currentProvider = DataRepository.Provider;
            bool useEntityFactory = currentProvider.UseEntityFactory;
            bool enableEntityTracking = currentProvider.EnableEntityTracking;
            LoadPolicy currentLoadPolicy = currentProvider.CurrentLoadPolicy;
			Type entityCreationFactoryType = currentProvider.EntityCreationalFactoryType;
			
			// advance to the starting row
			for (int i = 0; i < start; i++)
			{
				if (!reader.Read())
				return rows; // not enough rows, just return
			}
			for (int i = 0; i < pageLength; i++)
			{
				if (!reader.Read())
					break; // we are done
					
				string key = null;
				
				GPSTrackingDBV2.Entities.ScheduleWork c = null;
				if (useEntityFactory)
				{
					key = new System.Text.StringBuilder("ScheduleWork")
					.Append("|").Append((System.Int32)reader[((int)ScheduleWorkColumn.ScheduleWorkId - 1)]).ToString();
					c = EntityManager.LocateOrCreate<ScheduleWork>(
					key.ToString(), // EntityTrackingKey
					"ScheduleWork",  //Creational Type
					entityCreationFactoryType,  //Factory used to create entity
					enableEntityTracking); // Track this entity?
				}
				else
				{
					c = new GPSTrackingDBV2.Entities.ScheduleWork();
				}
				
				if (!enableEntityTracking ||
					c.EntityState == EntityState.Added ||
					(enableEntityTracking &&
					
						(
							(currentLoadPolicy == LoadPolicy.PreserveChanges && c.EntityState == EntityState.Unchanged) ||
							(currentLoadPolicy == LoadPolicy.DiscardChanges && c.EntityState != EntityState.Unchanged)
						)
					))
				{
					c.SuppressEntityEvents = true;
					c.ScheduleWorkId = (System.Int32)reader[((int)ScheduleWorkColumn.ScheduleWorkId - 1)];
					c.OriginalScheduleWorkId = c.ScheduleWorkId;
					c.DayId = (System.String)reader[((int)ScheduleWorkColumn.DayId - 1)];
					c.WorkTimeId = (System.Int32)reader[((int)ScheduleWorkColumn.WorkTimeId - 1)];
					c.EntityTrackingKey = key;
					c.AcceptChanges();
					c.SuppressEntityEvents = false;
				}
				rows.Add(c);
			}
		return rows;
		}		
		/// <summary>
		/// Refreshes the <see cref="GPSTrackingDBV2.Entities.ScheduleWork"/> object from the <see cref="IDataReader"/>.
		/// </summary>
		/// <param name="reader">The <see cref="IDataReader"/> to read from.</param>
		/// <param name="entity">The <see cref="GPSTrackingDBV2.Entities.ScheduleWork"/> object to refresh.</param>
		public static void RefreshEntity(IDataReader reader, GPSTrackingDBV2.Entities.ScheduleWork entity)
		{
			if (!reader.Read()) return;
			
			entity.ScheduleWorkId = (System.Int32)reader[((int)ScheduleWorkColumn.ScheduleWorkId - 1)];
			entity.OriginalScheduleWorkId = (System.Int32)reader["ScheduleWorkID"];
			entity.DayId = (System.String)reader[((int)ScheduleWorkColumn.DayId - 1)];
			entity.WorkTimeId = (System.Int32)reader[((int)ScheduleWorkColumn.WorkTimeId - 1)];
			entity.AcceptChanges();
		}
		
		/// <summary>
		/// Refreshes the <see cref="GPSTrackingDBV2.Entities.ScheduleWork"/> object from the <see cref="DataSet"/>.
		/// </summary>
		/// <param name="dataSet">The <see cref="DataSet"/> to read from.</param>
		/// <param name="entity">The <see cref="GPSTrackingDBV2.Entities.ScheduleWork"/> object.</param>
		public static void RefreshEntity(DataSet dataSet, GPSTrackingDBV2.Entities.ScheduleWork entity)
		{
			DataRow dataRow = dataSet.Tables[0].Rows[0];
			
			entity.ScheduleWorkId = (System.Int32)dataRow["ScheduleWorkID"];
			entity.OriginalScheduleWorkId = (System.Int32)dataRow["ScheduleWorkID"];
			entity.DayId = (System.String)dataRow["DayID"];
			entity.WorkTimeId = (System.Int32)dataRow["WorkTimeID"];
			entity.AcceptChanges();
		}
		#endregion 
		
		#region DeepLoad Methods
		/// <summary>
		/// Deep Loads the <see cref="IEntity"/> object with criteria based of the child 
		/// property collections only N Levels Deep based on the <see cref="DeepLoadType"/>.
		/// </summary>
		/// <remarks>
		/// Use this method with caution as it is possible to DeepLoad with Recursion and traverse an entire object graph.
		/// </remarks>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="entity">The <see cref="GPSTrackingDBV2.Entities.ScheduleWork"/> object to load.</param>
		/// <param name="deep">Boolean. A flag that indicates whether to recursively save all Property Collection that are descendants of this instance. If True, saves the complete object graph below this object. If False, saves this object only. </param>
		/// <param name="deepLoadType">DeepLoadType Enumeration to Include/Exclude object property collections from Load.</param>
		/// <param name="childTypes">GPSTrackingDBV2.Entities.ScheduleWork Property Collection Type Array To Include or Exclude from Load</param>
		/// <param name="innerList">A collection of child types for easy access.</param>
	    /// <exception cref="ArgumentNullException">entity or childTypes is null.</exception>
	    /// <exception cref="ArgumentException">deepLoadType has invalid value.</exception>
		public override void DeepLoad(TransactionManager transactionManager, GPSTrackingDBV2.Entities.ScheduleWork entity, bool deep, DeepLoadType deepLoadType, System.Type[] childTypes, DeepSession innerList)
		{
			if(entity == null)
				return;

			#region DayIdSource	
			if (CanDeepLoad(entity, "DayNames|DayIdSource", deepLoadType, innerList) 
				&& entity.DayIdSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = entity.DayId;
				DayNames tmpEntity = EntityManager.LocateEntity<DayNames>(EntityLocator.ConstructKeyFromPkItems(typeof(DayNames), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.DayIdSource = tmpEntity;
				else
					entity.DayIdSource = DataRepository.DayNamesProvider.GetByDayId(transactionManager, entity.DayId);		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'DayIdSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.DayIdSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.DayNamesProvider.DeepLoad(transactionManager, entity.DayIdSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion DayIdSource

			#region WorkTimeIdSource	
			if (CanDeepLoad(entity, "WorkTime|WorkTimeIdSource", deepLoadType, innerList) 
				&& entity.WorkTimeIdSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = entity.WorkTimeId;
				WorkTime tmpEntity = EntityManager.LocateEntity<WorkTime>(EntityLocator.ConstructKeyFromPkItems(typeof(WorkTime), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.WorkTimeIdSource = tmpEntity;
				else
					entity.WorkTimeIdSource = DataRepository.WorkTimeProvider.GetByWorkTimeId(transactionManager, entity.WorkTimeId);		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'WorkTimeIdSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.WorkTimeIdSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.WorkTimeProvider.DeepLoad(transactionManager, entity.WorkTimeIdSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion WorkTimeIdSource
			
			//used to hold DeepLoad method delegates and fire after all the local children have been loaded.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();
			// Deep load child collections  - Call GetByScheduleWorkId methods when available
			
			#region ScheduleWorkByCompaniesCollection
			//Relationship Type One : Many
			if (CanDeepLoad(entity, "List<ScheduleWorkByCompanies>|ScheduleWorkByCompaniesCollection", deepLoadType, innerList)) 
			{
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'ScheduleWorkByCompaniesCollection' loaded. key " + entity.EntityTrackingKey);
				#endif 

				entity.ScheduleWorkByCompaniesCollection = DataRepository.ScheduleWorkByCompaniesProvider.GetByScheduleWorkId(transactionManager, entity.ScheduleWorkId);

				if (deep && entity.ScheduleWorkByCompaniesCollection.Count > 0)
				{
					deepHandles.Add("ScheduleWorkByCompaniesCollection",
						new KeyValuePair<Delegate, object>((DeepLoadHandle<ScheduleWorkByCompanies>) DataRepository.ScheduleWorkByCompaniesProvider.DeepLoad,
						new object[] { transactionManager, entity.ScheduleWorkByCompaniesCollection, deep, deepLoadType, childTypes, innerList }
					));
				}
			}		
			#endregion 
			
			
			#region CompanyIdCompaniesCollection_From_ScheduleWorkByCompanies
			// RelationshipType.ManyToMany
			if (CanDeepLoad(entity, "List<Companies>|CompanyIdCompaniesCollection_From_ScheduleWorkByCompanies", deepLoadType, innerList))
			{
				entity.CompanyIdCompaniesCollection_From_ScheduleWorkByCompanies = DataRepository.CompaniesProvider.GetByScheduleWorkIdFromScheduleWorkByCompanies(transactionManager, entity.ScheduleWorkId);			 
		
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'CompanyIdCompaniesCollection_From_ScheduleWorkByCompanies' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.CompanyIdCompaniesCollection_From_ScheduleWorkByCompanies != null)
				{
					deepHandles.Add("CompanyIdCompaniesCollection_From_ScheduleWorkByCompanies",
						new KeyValuePair<Delegate, object>((DeepLoadHandle< Companies >) DataRepository.CompaniesProvider.DeepLoad,
						new object[] { transactionManager, entity.CompanyIdCompaniesCollection_From_ScheduleWorkByCompanies, deep, deepLoadType, childTypes, innerList }
					));
				}
			}
			#endregion
			
			
			
			//Fire all DeepLoad Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			deepHandles = null;
		}
		
		#endregion 
		
		#region DeepSave Methods

		/// <summary>
		/// Deep Save the entire object graph of the GPSTrackingDBV2.Entities.ScheduleWork object with criteria based of the child 
		/// Type property array and DeepSaveType.
		/// </summary>
		/// <param name="transactionManager">The transaction manager.</param>
		/// <param name="entity">GPSTrackingDBV2.Entities.ScheduleWork instance</param>
		/// <param name="deepSaveType">DeepSaveType Enumeration to Include/Exclude object property collections from Save.</param>
		/// <param name="childTypes">GPSTrackingDBV2.Entities.ScheduleWork Property Collection Type Array To Include or Exclude from Save</param>
		/// <param name="innerList">A Hashtable of child types for easy access.</param>
		public override bool DeepSave(TransactionManager transactionManager, GPSTrackingDBV2.Entities.ScheduleWork entity, DeepSaveType deepSaveType, System.Type[] childTypes, DeepSession innerList)
		{	
			if (entity == null)
				return false;
							
			#region Composite Parent Properties
			//Save Source Composite Properties, however, don't call deep save on them.  
			//So they only get saved a single level deep.
			
			#region DayIdSource
			if (CanDeepSave(entity, "DayNames|DayIdSource", deepSaveType, innerList) 
				&& entity.DayIdSource != null)
			{
				DataRepository.DayNamesProvider.Save(transactionManager, entity.DayIdSource);
				entity.DayId = entity.DayIdSource.DayId;
			}
			#endregion 
			
			#region WorkTimeIdSource
			if (CanDeepSave(entity, "WorkTime|WorkTimeIdSource", deepSaveType, innerList) 
				&& entity.WorkTimeIdSource != null)
			{
				DataRepository.WorkTimeProvider.Save(transactionManager, entity.WorkTimeIdSource);
				entity.WorkTimeId = entity.WorkTimeIdSource.WorkTimeId;
			}
			#endregion 
			#endregion Composite Parent Properties

			// Save Root Entity through Provider
			if (!entity.IsDeleted)
				this.Save(transactionManager, entity);
			
			//used to hold DeepSave method delegates and fire after all the local children have been saved.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();

			#region CompanyIdCompaniesCollection_From_ScheduleWorkByCompanies>
			if (CanDeepSave(entity.CompanyIdCompaniesCollection_From_ScheduleWorkByCompanies, "List<Companies>|CompanyIdCompaniesCollection_From_ScheduleWorkByCompanies", deepSaveType, innerList))
			{
				if (entity.CompanyIdCompaniesCollection_From_ScheduleWorkByCompanies.Count > 0 || entity.CompanyIdCompaniesCollection_From_ScheduleWorkByCompanies.DeletedItems.Count > 0)
				{
					DataRepository.CompaniesProvider.Save(transactionManager, entity.CompanyIdCompaniesCollection_From_ScheduleWorkByCompanies); 
					deepHandles.Add("CompanyIdCompaniesCollection_From_ScheduleWorkByCompanies",
						new KeyValuePair<Delegate, object>((DeepSaveHandle<Companies>) DataRepository.CompaniesProvider.DeepSave,
						new object[] { transactionManager, entity.CompanyIdCompaniesCollection_From_ScheduleWorkByCompanies, deepSaveType, childTypes, innerList }
					));
				}
			}
			#endregion 
	
			#region List<ScheduleWorkByCompanies>
				if (CanDeepSave(entity.ScheduleWorkByCompaniesCollection, "List<ScheduleWorkByCompanies>|ScheduleWorkByCompaniesCollection", deepSaveType, innerList)) 
				{	
					// update each child parent id with the real parent id (mostly used on insert)
					foreach(ScheduleWorkByCompanies child in entity.ScheduleWorkByCompaniesCollection)
					{
						if(child.ScheduleWorkIdSource != null)
						{
								child.ScheduleWorkId = child.ScheduleWorkIdSource.ScheduleWorkId;
						}

						if(child.CompanyIdSource != null)
						{
								child.CompanyId = child.CompanyIdSource.CompanyId;
						}

					}

					if (entity.ScheduleWorkByCompaniesCollection.Count > 0 || entity.ScheduleWorkByCompaniesCollection.DeletedItems.Count > 0)
					{
						//DataRepository.ScheduleWorkByCompaniesProvider.Save(transactionManager, entity.ScheduleWorkByCompaniesCollection);
						
						deepHandles.Add("ScheduleWorkByCompaniesCollection",
						new KeyValuePair<Delegate, object>((DeepSaveHandle< ScheduleWorkByCompanies >) DataRepository.ScheduleWorkByCompaniesProvider.DeepSave,
							new object[] { transactionManager, entity.ScheduleWorkByCompaniesCollection, deepSaveType, childTypes, innerList }
						));
					}
				} 
			#endregion 
				
			//Fire all DeepSave Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			
			// Save Root Entity through Provider, if not already saved in delete mode
			if (entity.IsDeleted)
				this.Save(transactionManager, entity);
				

			deepHandles = null;
						
			return true;
		}
		#endregion
	} // end class
	
	#region ScheduleWorkChildEntityTypes
	
	///<summary>
	/// Enumeration used to expose the different child entity types 
	/// for child properties in <c>GPSTrackingDBV2.Entities.ScheduleWork</c>
	///</summary>
	public enum ScheduleWorkChildEntityTypes
	{
		
		///<summary>
		/// Composite Property for <c>DayNames</c> at DayIdSource
		///</summary>
		[ChildEntityType(typeof(DayNames))]
		DayNames,
			
		///<summary>
		/// Composite Property for <c>WorkTime</c> at WorkTimeIdSource
		///</summary>
		[ChildEntityType(typeof(WorkTime))]
		WorkTime,
	
		///<summary>
		/// Collection of <c>ScheduleWork</c> as OneToMany for ScheduleWorkByCompaniesCollection
		///</summary>
		[ChildEntityType(typeof(TList<ScheduleWorkByCompanies>))]
		ScheduleWorkByCompaniesCollection,

		///<summary>
		/// Collection of <c>ScheduleWork</c> as ManyToMany for CompaniesCollection_From_ScheduleWorkByCompanies
		///</summary>
		[ChildEntityType(typeof(TList<Companies>))]
		CompanyIdCompaniesCollection_From_ScheduleWorkByCompanies,
	}
	
	#endregion ScheduleWorkChildEntityTypes
	
	#region ScheduleWorkFilterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="SqlFilterBuilder&lt;ScheduleWorkColumn&gt;"/> class
	/// that is used exclusively with a <see cref="ScheduleWork"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class ScheduleWorkFilterBuilder : SqlFilterBuilder<ScheduleWorkColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the ScheduleWorkFilterBuilder class.
		/// </summary>
		public ScheduleWorkFilterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the ScheduleWorkFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public ScheduleWorkFilterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the ScheduleWorkFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public ScheduleWorkFilterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion ScheduleWorkFilterBuilder
	
	#region ScheduleWorkParameterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="ParameterizedSqlFilterBuilder&lt;ScheduleWorkColumn&gt;"/> class
	/// that is used exclusively with a <see cref="ScheduleWork"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class ScheduleWorkParameterBuilder : ParameterizedSqlFilterBuilder<ScheduleWorkColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the ScheduleWorkParameterBuilder class.
		/// </summary>
		public ScheduleWorkParameterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the ScheduleWorkParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public ScheduleWorkParameterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the ScheduleWorkParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public ScheduleWorkParameterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion ScheduleWorkParameterBuilder
	
	#region ScheduleWorkSortBuilder
    
    /// <summary>
    /// A strongly-typed instance of the <see cref="SqlSortBuilder&lt;ScheduleWorkColumn&gt;"/> class
	/// that is used exclusively with a <see cref="ScheduleWork"/> object.
    /// </summary>
    [CLSCompliant(true)]
    public class ScheduleWorkSortBuilder : SqlSortBuilder<ScheduleWorkColumn>
    {
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the ScheduleWorkSqlSortBuilder class.
		/// </summary>
		public ScheduleWorkSortBuilder() : base() { }

		#endregion Constructors

    }    
    #endregion ScheduleWorkSortBuilder
	
} // end namespace
